package org.example;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;

public class Sender extends JFrame implements ActionListener {
    JLabel Lsen=null, Lget=null, Lobj=null, Ldata, Lser=null;
    JTextField Tsen=null, Tget=null, Tobj=null;
    JScrollPane jsp1, jsp2 = null;
    JPanel jp1, jp2,jp3,jp4,jp5,jp6 = null;
    JTextArea area1, area2= null;
    JButton Bback=null, Bsend=null;
    Socket socket;
    InputStream input;
    OutputStream output;
    BufferedReader reader;
    PrintWriter writer;
    String ser_ip;
    String sender;
    String password;
    public Sender(String ser_ip1, String sender1, String password1)
    {
        ser_ip= ser_ip1;
        sender= sender1;
        password = password1;

        Lsen= new JLabel("发件人");
        Lget= new JLabel("收件人");
        Lobj= new JLabel("主题");
        Ldata= new JLabel("正文");
        Lser= new JLabel("响应");

        Tsen= new JTextField(15);
        Tsen.setText(sender);
        Tget= new JTextField(15);
        Tobj= new JTextField(20);

        Bback= new JButton("返回");
        Bsend= new JButton("发送");
        Bback.addActionListener(this);
        Bsend.addActionListener(this);

        area1= new JTextArea(20,70);
        area2= new JTextArea(7, 70);
        area1.setVisible(true);
        area2.setVisible(true);
        jsp1 = new JScrollPane();
        jsp1.setViewportView(area1);
        jsp2 = new JScrollPane();
        jsp2.setViewportView(area2);

        jp1 = new JPanel(); jp2 = new JPanel(); jp3 = new JPanel(); jp4 = new JPanel(); jp5 = new JPanel(); jp6 = new JPanel();

        jp1.add(Lsen); jp1.add(Tsen); jp1.add(Lget); jp1.add(Tget);
        jp2.add(Lobj); jp2.add(Tobj); jp2.add(Bsend); jp2.add(Bback);
        jp3.add(Ldata);
        jp4.add(jsp1);
        jp5.add(Lser);
        jp6.add(jsp2);

        jp1.setBounds(50,10, 800,40);
        jp2.setBounds(50,60, 800,40);
        jp3.setBounds(50,110, 800,30);
        jp4.setBounds(50,150, 800,350);
        jp5.setBounds(50,510, 800,30);
        jp6.setBounds(50,550, 800,150);

        this.add(jp1); this.add(jp2); this.add(jp3); this.add(jp4); this.add(jp5); this.add(jp6);

//        this.setLayout(new GridLayout(6,1));
        this.setLayout(null);
        this.setTitle("邮箱极简版————发送邮件");
        this.setSize(900, 720);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setResizable(false);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("发送") ) {
            sender= Tsen.getText();
            System.out.println(sender);
            String receiver= Tget.getText();
            String subject= Tobj.getText();
            String ID = Base64.ToBase64(sender.getBytes());
            String pass = Base64.ToBase64(password.getBytes());
            try {
                socket = new Socket(ser_ip, 25);
                input = socket.getInputStream();
                output = socket.getOutputStream();
                reader = new BufferedReader(new InputStreamReader(input));
                writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "gbk"), true);
                area2.setText(">telnet "+ser_ip+" 25"+ "\n");
                area2.append(reader.readLine()+ "\n");

                writer.println("HELO gxy");
                area2.append(">HELO gxy"+ "\n");
                area2.append(reader.readLine()+ "\n");
                area2.append(reader.readLine()+ "\n");
                area2.append(reader.readLine()+ "\n");

                //AUTH LOGIN
                writer.println("auth login");
                area2.append(">auth login"+ "\n");
                area2.append(reader.readLine()+ "\n");
                writer.println(ID);
                area2.append(reader.readLine()+ "\n");
                writer.println(pass);
                area2.append(reader.readLine()+ "\n");
                writer.println("MAIL FROM:<" + sender +">");
                area2.append(">MAIL FROM:<" + sender +">"+ "\n");
                area2.append(reader.readLine()+ "\n");
                writer.println("RCPT TO:<" + receiver +">");
                area2.append(">RCPT TO:<" + receiver +">"+ "\n");
                area2.append(reader.readLine()+ "\n");

                //Set data
                writer.println("data");
                area2.append(">data"+ "\n");
                area2.append(reader.readLine()+ "\n");
                writer.println("subject:" + subject);
                writer.println("from:" + sender);
                writer.println("to:" + receiver);
                writer.println();
                writer.println(area1.getText());
                writer.println(".");
                writer.println("");
                area2.append(reader.readLine()+ "\n");

                writer.println("quit");
                area2.append(">quit"+ "\n");
                area2.append(reader.readLine()+ "\n");
                input.close();
                output.close();
                socket.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        else if (e.getActionCommand().equals("返回") ) {
            dispose();
        }
    }
}
